﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using IronOne.SalesOne.BusinessObjects;
using System.Linq.Dynamic;
using System.Configuration;


namespace IronOne.SalesOne.DataObjects
{
    public class LinqComplaint
    {
        public int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]);

        public Complaint GetComplaint(string ComplaintId)
        {

            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                return Mapper.ToComplaintObject(db.ComplaintEntities
                            .SingleOrDefault(p => p.ComplaintId == ComplaintId));
            }
        }

        public List<Complaint> SearchComplaint(SearchCS comp,int pageNo)
        {
            StringBuilder sb = new StringBuilder();
            //complaint table

            if (!String.IsNullOrEmpty(comp.ContractId))
                sb.Append(" AND ContractNo = \"" + comp.ContractId + "\"");
            if (!String.IsNullOrEmpty(comp.ClientId))
                sb.Append(" AND CustomerId = \"" + comp.ClientId + "\"");
            if (!String.IsNullOrEmpty(comp.Status))
                sb.Append(" AND ComplaintStatus = " + int.Parse(comp.Status));
            if (!String.IsNullOrEmpty(comp.ClaimNo))
                sb.Append(" AND ComplaintId = \"" + comp.ClaimNo + "\"");



            StringBuilder str = new StringBuilder();

            //unit
            if (comp.Unt != null)
            {
                int j = 0;
                foreach (SalesOneUnit un in comp.Unt)
                {

                    if (j == 0)
                    {
                        if (un.UnitId != 0)
                            str.Append(" AND (NodeId=" + un.UnitId.ToString() + " AND UserTypeId = 4)");

                    }
                    else
                    {
                        if (un.UnitId != 0)
                            str.Append(" OR (NodeId=" + un.UnitId.ToString() + " AND UserTypeId = 4)");

                    }

                    j++;
                }
            }



            //branch
            if (comp.Brnch != null)
            {
                int k = 0;
                foreach (Branch br in comp.Brnch)
                {

                    if (k == 0)
                    {
                        if (br.BranchId != 0 && comp.Unt == null)
                            str.Append(" AND (NodeId=" + br.BranchId.ToString() + " AND UserTypeId = 3)");
                    }
                    else
                    {
                        if (br.BranchId != 0 && comp.Unt == null)
                            str.Append(" OR (NodeId=" + br.BranchId.ToString() + " AND UserTypeId = 3)");
                    }

                    k++;
                }
            }



            //so
            if (comp.SOCode != null)
            {
                int i = 0;
                foreach (string so in comp.SOCode)
                {
                    if (i == 0)
                    {
                        if (!String.IsNullOrEmpty(so) && so != "Select")
                            sb.Append(" AND SOCode = \"" + so + "\"");
                    }
                    else
                    {
                        if (!String.IsNullOrEmpty(so) && so != "Select")
                            sb.Append(" OR SOCode = \"" + so + "\"");
                    }

                    i++;
                }
            }


            if (sb.Length > 1 || str.Length > 1)
            {
                if (sb.ToString().StartsWith(" AND "))
                    sb.Remove(0, 5);
                if (str.ToString().StartsWith(" AND "))
                    str.Remove(0, 5);


                using (SODataDataContext db = DataContextFactory.CreateContext())
                {
                    IQueryable<ComplaintEntity> query = db.ComplaintEntities;



                    if (sb.Length > 1 && str.Length > 1)
                    {
                        query = from complaint in (db.ComplaintEntities.Where(sb.ToString()))
                                join u in db.UserEntities on complaint.SOCode equals u.SOCode
                                join us in db.UsersInStructureEntities.Where(str.ToString())
                                on u.UserId equals us.UserId
                                select complaint;

                        //query = from complaint in (db.ComplaintEntities.Where(sb.ToString()))
                        //        join cust in
                        //            (from c in db.CustomerEntities
                        //             join u in db.UserEntities on c.SOCode equals u.SOCode
                        //             join us in db.UsersInStructureEntities.Where(str.ToString())
                        //             on u.UserId equals us.UserId
                        //             select c)
                        //        on complaint.CustomerId equals cust.CustomerID
                        //        select complaint;

                    }
                    else if (sb.Length > 1 && str.Length < 1)
                    {

                        query = from complaint in (db.ComplaintEntities.Where(sb.ToString()))
                                select complaint;

                        //query = from complaint in (db.ComplaintEntities.Where(sb.ToString()))
                        //        join cust in db.CustomerEntities
                        //        on complaint.CustomerId equals cust.CustomerID
                        //        select complaint;
                    }
                    else if (sb.Length < 1 && str.Length > 1)
                    {

                        query = from complaint in db.ComplaintEntities
                                join u in db.UserEntities on complaint.SOCode equals u.SOCode
                                join us in db.UsersInStructureEntities.Where(str.ToString())
                                on u.UserId equals us.UserId
                                select complaint;


                        //query = from complaint in db.ComplaintEntities
                        //        join cust in
                        //            (from c in db.CustomerEntities
                        //             join u in db.UserEntities on c.SOCode equals u.SOCode
                        //             join us in db.UsersInStructureEntities.Where(str.ToString())
                        //             on u.UserId equals us.UserId
                        //             select c)
                        //        on complaint.CustomerId equals cust.CustomerID
                        //        select complaint;
                    }

                    IQueryable<ComplaintEntity> pageData = query.Skip((pageNo - 1) * pageSize).Take(pageSize);

                    return pageData.Select(c => Mapper.ToComplaintObject(c)).ToList();
                }
            }
            else
                return null;
        }


        public int UpdateComplaint(Complaint comp)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    ComplaintEntity entity = Mapper.ToComplaintEntity(comp);
                    db.ComplaintEntities.Attach(entity, true);
                    db.SubmitChanges();
                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to update policy record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }
    }
}
